﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRMAdminDashboard.CrmSvc;

namespace CRMAdminDashboard
{
    /// <summary>
    /// Serializable version of SystemRole
    /// </summary>
    [Serializable]
    public class SerializableRole : role
    {

        #region Inner Classes

        /// <summary>
        /// Privilege for role.
        /// </summary>
        public class SerializableRolePrivilege
        {

            #region Properties

            public privilege Privilege { get; set; }

            public PrivilegeDepth Depth { get; set; }

            #endregion

            #region Entry Points

            public SerializableRolePrivilege() { }

            public SerializableRolePrivilege(RolePrivilege rp, Dictionary<Guid, SystemPrivilege> allPrivilegesByGuid)
            {
                this.Depth = rp.Depth;
                this.Privilege = allPrivilegesByGuid[rp.PrivilegeId].InnerPrivilege;
            }

            #endregion

        }

        #endregion

        #region Properties

        public List<SerializableUser> Users { get; set; }

        public List<SerializableRolePrivilege> RolePrivileges { get; set; }

        #endregion

        #region Entry Points

        /// <summary>
        /// Make a serializable role out of SystemRole, include privileges
        /// </summary>
        public SerializableRole(SystemRole srole, Dictionary<Guid, SystemPrivilege> allPrivilegesByGuid)
            : this(srole)
        {
            this.RolePrivileges = new List<SerializableRolePrivilege>();
            foreach (RolePrivilege rp in srole.RolePrivileges.Values)
            {
                this.RolePrivileges.Add(new SerializableRolePrivilege(rp, allPrivilegesByGuid));
            }
        }

        /// <summary>
        /// Make a serializable role out of SystemRole
        /// </summary>
        public SerializableRole(SystemRole srole)
        {
            this.businessunitid = srole.InnerRole.businessunitid;
            this.createdby = srole.InnerRole.createdby;
            this.createdon = srole.InnerRole.createdon;
            this.importsequencenumber = srole.InnerRole.importsequencenumber;
            this.modifiedby = srole.InnerRole.modifiedby;
            this.modifiedon = srole.InnerRole.modifiedon;
            this.name = srole.InnerRole.name;
            this.organizationid = srole.InnerRole.organizationid;
            this.overriddencreatedon = srole.InnerRole.overriddencreatedon;
            this.parentroleid = srole.InnerRole.parentroleid;
            this.roleid = srole.InnerRole.roleid;
            this.roletemplateid = srole.InnerRole.roletemplateid;
        }

        /// <summary>
        /// Make a serializable role out of SystemRole, include associated users
        /// </summary>
        public SerializableRole(CachingCrmService svcHandle, SystemRole srole, RoleUserHierarchy rUHierarchy)
            : this(srole)
        {
            Dictionary<Guid, SystemUser> users = srole.GetUsers(svcHandle, new AllColumns(), rUHierarchy);
            this.Users = new List<SerializableUser>();
            foreach (SystemUser user in users.Values)
            {
                this.Users.Add(new SerializableUser(user));
            }
        }

        /// <summary>
        /// Make a serializable role out of SystemRole, include privileges & associated users
        /// </summary>
        public SerializableRole(CachingCrmService svcHandle, SystemRole srole, RoleUserHierarchy rUHierarchy,
            Dictionary<Guid, SystemPrivilege> allPrivilegesByGuid)
            : this(svcHandle, srole, rUHierarchy)
        {
            this.RolePrivileges = new List<SerializableRolePrivilege>();
            foreach (RolePrivilege rp in srole.RolePrivileges.Values)
            {
                this.RolePrivileges.Add(new SerializableRolePrivilege(rp, allPrivilegesByGuid));
            }
        }

        /// <summary>
        /// Needed for the serializer
        /// </summary>
        public SerializableRole() { }

        #endregion

    }
}
